-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
groq-builder: implement "conditionals" feature #252
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
… uses 2nd parameter
* feature(fragment): added `q.fragment` implementation * feature(fragment): added tests for fragment queries * feature(fragment): ensure we export `Fragment` and `InferFragmentType` types * feature(fragment): added docs * feature(validation): fixed broken import --------- Co-authored-by: scottrippey <scott.william.rippey@gmail.com>
…re unique, spreadable keys
# Conflicts: # packages/groq-builder/src/commands/fragment.test.ts # packages/groq-builder/src/commands/projection-types.ts # packages/groq-builder/src/types/public-types.ts # packages/groq-builder/src/types/utils.ts
carbonrobot
approved these changes
Jan 17, 2024
maxyinger
approved these changes
Jan 17, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have time to fully go through and understand the internal of this atm. But the outward facing api looks awesome and glancing through the test cases it all looks good to me. Awesome work
This was referenced Jan 18, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Adds methods to create "conditional" projections.
Docs: (copied from CONDITIONALS.md)
Conditionals
In Groq, there are 2 ways to use conditional logic: inline in a projection, or using the
select
function.Conditions in a projection
In
groq-builder
, theproject
method allows inline conditional statements with the help ofq.conditional$(...)
orq.conditionalByType(...)
using the following syntax:This outputs the following groq query:
And the result type is inferred as:
Notice that the conditions are wrapped in
q.conditional$()
and then spread into the projection. This is necessary for type-safety and runtime validation.The
$
in the methodq.conditional$
indicates that this method is not completely type-safe; the condition statements (eg._type == 'movie'
) are not strongly-typed (this may be improved in a future version).However, the most common use-case is to base conditional logic off the document's
_type
. For this, we have theq.conditionalByType
helper:Strongly-typed conditions via
q.conditionalByType(...)
The most common use-case for conditional logic is to check the
_type
field.The
q.conditionalByType(...)
method makes this easier, by ensuring all conditional logic is strongly-typed, and it enables auto-complete. For example:The resulting query is identical to the above example with
q.conditional$
.The result type here is inferred as:
Notice that this type is stronger than the example with
q.conditional$
, because we've detected that the conditions are "exhaustive".The
select
methodAdds support for the
select$
method:The
$
sign is to indicate that there's some "loosely typed" code in here -- the conditions are unchecked.This will output the following query:
And will have the following result type:
The
selectByType
methodAdds a
selectByType
helper, which facilitates type-based logic. This is completely strongly-typed: